package cn.itsub.sodo.action;

import java.util.ArrayList;
import java.util.List;

import cn.itsub.sodo.Node;
import cn.itsub.sodo.action.interval.IntervalAction;
import cn.itsub.sodo.Sprite;
import cn.itsub.sodo.cache.TextureCache;
import cn.itsub.sodo.core.Texture2D;

public class Animate extends IntervalAction {
	public static Animate create(float duration){
		return new Animate(duration);
	}
	protected Animate(float duration) {
		super(duration);
	}
	public List<Texture2D> imgs = new ArrayList<Texture2D>();

	public void addFrame(String fileName){
		Texture2D tx = TextureCache.getInstance().add(fileName);
		tx.initWithFileName(fileName);
		this.imgs.add(tx);
	}
	@Override
	public void start(final Node target) {
		super.start(target);
	}
	
	public IntervalAction copy() {
		Animate ani = new Animate(duration);
		ani.imgs = new ArrayList<Texture2D>();
		ani.imgs.addAll(this.imgs);
		return ani;
	}
	@Override
	public void update(float time) {
		
		int index = (int)(time*imgs.size());
		if(index>=imgs.size()){
			index--;
		}
		Sprite sprite = (Sprite) target;
		sprite.setTexture(imgs.get(index));
		
	}
	
}
